/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.framework
{
	import funit.errors.AssertionError;
	import funit.framework.comparers.AssertEqualityComparer;
	import funit.framework.writers.EqualMessageWriter;
	import funit.framework.writers.MessageWriter;
	
	import sv.collections.IEqualityComparer;
	
	public class StringAssert
	{
		
		/**
		 * @param value
		 * @param message
		 * @param messageParameters
		 */
		public static function isEmpty( value:String, message:String = "", ... messageParameters ) : void
		{
			if ( value != "" )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine("");
				writer.writeActualLine(value);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param value
		 * @param message
		 * @param messageParameters
		 */
		public static function isNotEmpty( value:String, message:String = "", ... messageParameters ) : void
		{
			if ( value == "" )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine("", "not");
				writer.writeActualLine(value);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param substring
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function contains( substring:String, actual:String, message:String = "", ... messageParameters ) : void
		{
			if (actual.indexOf(substring) == -1)
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(substring, "String containing");
				writer.writeActualLine(actual);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param starting
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function startsWith( starting:String, actual:String, message:String = "", ... messageParameters ) : void
		{
			if ( starting != actual &&
				 actual.indexOf(starting) != 0 )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(starting, "String starting with");
				writer.writeActualLine(actual);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param ending
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function endsWith( ending:String, actual:String, message:String = "", ... messageParameters ) : void
		{
			if ( ending != actual &&
				 actual.lastIndexOf(ending) != (actual.length - ending.length) )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(ending, "String ending with");
				writer.writeActualLine(actual);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param expected
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function areEqualIgnoringCase( expected:String, actual:String, message:String = "", ... messageParameters ) : void
		{
			var comparer:IEqualityComparer = new AssertEqualityComparer(true);
			
			if ( !comparer.equals(expected, actual) )
			{
				var writer:EqualMessageWriter = new EqualMessageWriter( message, messageParameters );
				var mismatch:int = MessageUtils.findMismatchPosition(expected, actual, 0, true);
				writer.displayStringDifferences(expected, actual, mismatch, true);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * 
		 * @param expression
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function isMatch( expression:String, actual:String, message:String = "", ... messageParameters ) : void
		{
			var matches:Array = actual.match( new RegExp(expression) );
			
			if ( matches == null || matches.length == 0 )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(expression, "String matching");
				writer.writeActualLine(actual);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
	}
	
}